﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HsTec.DigitalPlatform.Model.DTO.UserModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>用户管理</title>
    <link href="<%=Url.Content("~/Content/page.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Content/flexigrid.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Content/contextmenu.css") %>" rel="stylesheet" type="text/css" />
    <link href="../../Content/redmond/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div>
        <table id="list" style="display: none">
        </table>
    </div>
    <div id="dlg-edit" style="display: none;">
        <% Html.RenderPartial("Edit"); %></div>
    <div id="dlgsearch" style="display: none;">
        <table border="0">
            <tr>
                <td>
                    用户名：
                </td>
                <td>
                    <input type="text" name="schName" id="schName" />
                </td>
            </tr>
            <tr>
                <td>
                    部门：
                </td>
                <td>
                    <%=Html.DropDownListFor(t => t.deptid, Model.deptlist, "请选择...", new { name="schDept",id="schDept"})%>
                </td>
            </tr>
            <tr>
                <td>
                    角色：
                </td>
                <td>
                    <input type="text" name="schRole" id="schRole" />
                </td>
            </tr>
        </table>
    </div>
    <%--<script src="<%=Url.Content("~/Scripts/jquery-1.4.4.js") %>" type="text/javascript"></script>--%>

    <script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>

    <script src="<%=Url.Content("~/Scripts/Plugins/jquery.flexigrid.js") %>" type="text/javascript"></script>

    <script src="<%=Url.Content("~/Scripts/Plugins/jquery.contextmenu.js") %>" type="text/javascript"></script>

    <script src="../../Scripts/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        var grid;
        $(document).ready(function() {
            var h = document.documentElement.clientHeight - 107;
            var option = {
                height: h,
                width: 'auto',
                url: 'User/GetJsonGridData',
                dataType: 'json',
                colModel: [
                { display: '用户ID', name: 'userid', width: 60, sortable: true, align: 'left', hide: true },
			        { display: '账户名', name: 'username', width: 100, sortable: true, align: 'left' },
			        { display: '用户名', name: 'useralias', width: 120, sortable: false, align: 'left' },
			        { display: '密码', name: 'password', width: 100, sortable: true, align: 'left' },
			        { display: '角色', name: 'roleid', width: 100, sortable: true, align: 'left' },
			        { display: '部门', name: 'deptid', width: 100, sortable: true, align: 'left' },
			        { display: '创建人', name: 'creater', width: 100, sortable: true, align: 'left' },
			        { display: '创建日期', name: 'createtime', width: 100, sortable: true, align: 'left' },
			        { display: '最后登录日期', name: 'lastlogindate', width: 100, sortable: true, align: 'left' }
				],
                buttons: [
                  { name: 'Add', displayname: "新增", bclass: 'Add', onpress: toolbarItem_onclick },
                  { name: 'Edit', displayname: "编辑", bclass: 'Add', onpress: toolbarItem_onclick },
                  { name: 'Delete', displayname: "删除", bclass: 'Delete', onpress: toolbarItem_onclick },
                  { name: 'Search', displayname: "查询", bclass: 'Add', onpress: toolbarItem_onclick },
                  { name: 'Export', displayname: "导出", bclass: 'Add', onpress: toolbarItem_onclick }
                ],
                sortname: "userid",
                sortorder: "asc",
                title: "部门列表",
                usepager: true,
                searchitems: [
                    { display: '用户名', name: 'username', isdefault: true }
                ],
                useRp: true,
                rp: 15,
                rpOptions: [10, 15, 20, 25, 40],
                rowbinddata: true,
                showcheckbox: true,
                rowhandler: contextmenu,
                selectedonclick: true,
                singleselected: false
            };
            grid = $("#list").flexigrid(option);
            function contextmenu(row) {
                var menu = { width: 150, items: [
                     { text: "查看", icon: "../../Content/images/icons/view.png", alias: "contextmenu-edit", action: contextMenuItem_click },
                     { text: "编辑", icon: "../../Content/images/icons/edit.png", alias: "contextmenu-view", action: contextMenuItem_click },
                     { text: "删除", icon: "../../Content/images/icons/rowdelete.png", alias: "contextmenu-delete", action: contextMenuItem_click },
                     { text: "刷新", icon: "../../Content/images/icons/table_refresh.png", alias: "contextmenu-reflash", action: contextMenuItem_click }
                ]
                };
                function contextMenuItem_click(target) {
                    var id = $(target).attr("id").substr(3);
                    var cmd = this.data.alias;
                    var ch = $.browser.msie ? target.ch : target.getAttribute("ch");
                    var cell = ch.split("_FG$SP_");
                    if (cmd == "contextmenu-edit") {
                        alert("编辑，产品编号=" + id);
                    }
                    else if (cmd == "contextmenu-view") {
                        alert("编辑，产品编号=" + id);
                    }
                    else if (cmd == "contextmenu-delete") {
                        var name = cell[1];
                        if (confirm("你确认要删除商品 [" + name + "] 吗？")) {
                            alert("删除，产品编号=" + id);
                        }
                    }
                    else {
                        $("#list").flexReload();
                    }
                }
                $(row).contextmenu(menu);
            }

            function toolbarItem_onclick(cmd, grid) {
                if (cmd == "Add") {
                    $("#ediform input[type='text']").each(function() {
                        $(this).val("");
                    });
                    $('#ediform').attr("action", "User/Add");
                    actions = "User/Add";
                    $("#dlg-edit").dialog('open');
                }
                else if (cmd == "Edit") {
                    selected_count = $('.trSelected', grid).length;
                    if (selected_count == 0) {
                        alert('请选择一条记录!');
                        return;
                    }
                    if (selected_count > 1) {
                        alert('抱歉只能同时修改一条记录!');
                        return;
                    }
                    data = new Array();
                    $('.trSelected td', grid).each(function(i) {
                        data[i] = $(this).children('div').text();
                    });
                    $('#ediform input[name="userid"]').val(data[1]);
                    $('#ediform input[name="username"]').val(data[2]);
                    $('#ediform input[name="useralias"]').val(data[3]);
                    $('#ediform input[name="password"]').val(data[4]);
                    $('#ediform select[name="roleid"]').val(data[5]);
                    $('#ediform select[name="deptid"]').val(data[6]);
                    $('#ediform input[name="creater"]').val(data[7]);
                    $('#ediform input[name="createtime"]').val(data[8]);
                    $('#ediform input[name="lastlogindate"]').val(data[9]);
                    $('#ediform').attr("action", "User/Edit");
                    actions = "User/Edit";
                    $("#dlg-edit").dialog('open');
                }
                else if (cmd == "Delete") {
                    selected_count = $('.trSelected', grid).length;
                    if (selected_count == 0) {
                        alert('请选择一条记录!');
                        return;
                    }
                    names = '';
                    $('.trSelected td:nth-child(3) div', grid).each(function(i) {
                        if (i)
                            names += ',';
                        names += $(this).text();
                    });
                    ids = '';
                    $('.trSelected td:nth-child(2) div', grid).each(function(i) {
                        if (i)
                            ids += ',';
                        ids += $(this).text();
                    })
                    if (confirm("确定删除用户[" + names + "]?")) {
                        delUser(ids);
                    }
                    $("#dlg-delete").dialog('open');
                }
                else if (cmd == "Export") {
                    document.location.href = "/User/Export/";
                }
                else if (cmd == "Search") {
                    $("#dlgsearch").dialog('open');
                }
            }
            function delUser(ids) {
                $.ajax({
                    url: 'User/Delete',
                    data: {
                        ids: ids
                    },
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        $('#list').flexReload();
                    },
                    error: function(data) {
                        alert("删除数据失败！");
                    }
                });
            }
            $("#dlg-edit").dialog({
                autoOpen: false,
                title: "编辑部门",
                height: 250,
                width: 250,
                modal: true,
                buttons: {
                    "保存": function() {
                        //有效性规则判断
                        if ($("#username").val() == "") {
                            $(".validateTips").html("请输入用户名");
                            return;
                        }
                        $("#btnSave").click();
                    },
                    "取消": function() {
                        $(this).dialog("close");
                    }

                }
            });
            $("#dlgsearch").dialog({
                autoOpen: false,
                title: "查询用户信息",
                height: 250,
                width: 250,
                modal: true,
                buttons: {
                    "保存": function() {
                        debugger;
                        $('#grid').flexOptions({
                            params: { username: $("#schName").val(), deptid: $("#schDept").val(), roleid: $("#schRole").val() }
                        }).flexReload();
                    },
                    "取消": function() {
                        $(this).dialog("close");
                    }

                }
            });
        });
        $(document).ready(ReSize);
        $(window).resize(ReSize);
        function ReSize() {
            var h = $(window).height() - 107;
            if (grid != null) {
                grid.resizeHeight(h);
            }
        } 
    </script>

</body>
</html>
